<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\UserModel;
use Illuminate\Http\Request;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $data = UserModel::paginate(config('page.pagesize'));

        return view('admin.user.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.user.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request,UserModel $userModel)
    {
        if ($request->isMethod('post')){
            $postData = $this->validate($request,[
                'username' => 'required|unique:users,username',
                'realname' => 'required',
                'email' => 'email',
                'phone' => 'required',
                'password' => 'required|confirmed',
            ],[
                'username.required' => '用户名不能为空',
                'username.unique' => '该用户名已存在,请重新定义',
                'truename.required' => '真实姓名不能为空',
                'password.required' => '密码不能为空',
                'password.confirmed' => '二次密码不一致',
                'phone.phone' => '请输入正确的手机号',
            ]);
            //密码加密
            $postData['password'] = bcrypt($postData['password']);
            //获取表单信息
            $post = $request->except(['_token','password_confirmation']);
            //用户入库
            $userModel = UserModel::create($post);
            $pwd = $post['password'];

            return redirect(route('admin.user.index'))->with('success','添加成功');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\UserModel  $userModel
     * @return \Illuminate\Http\Response
     */
    public function show(UserModel $userModel)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\UserModel  $userModel
     * @return \Illuminate\Http\Response
     */
    public function edit(UserModel $userModel)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\UserModel  $userModel
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, UserModel $userModel)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\UserModel  $userModel
     * @return \Illuminate\Http\Response
     */
    public function destroy(UserModel $userModel,$id)
    {
        $userModel->where('id',$id)->delete();
        return redirect(route('admin.user.index'));
    }
}
